package com.fbee.smartcommunities.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.fbee.smartcommunities.entity.Address;
import com.fbee.smartcommunities.mapper.AddressMapper;
import com.fbee.smartcommunities.service.AddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fbee.smartcommunities.utils.CustomizeException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 收货地址 服务实现类
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-23
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Resource
    private AddressMapper addressMapper;

    @Override
    @Transactional
    public Boolean setDefault(Long id) {
        if(!update(new UpdateWrapper<Address>().eq("is_default",1).set("is_default",0))){
            throw new CustomizeException("更改异常，请联系管理员");
        }
        if(!update(new UpdateWrapper<Address>().eq("id",id).set("is_default",1))){
            throw new CustomizeException("更改异常，请联系管理员");
        }
        return true;
    }
}
